import { Input } from 'antd-mobile';
import { useEffect } from 'react';
import { connect, useField } from '@formily/react';
import { Field } from '@formily/core';
import { defaultProps } from '../../designer/schema';
import './index.less';

const EformHidden = (props: any) => {
  const { addonAfter, autoComplete, inbiz,...others } = props;
  const isDesigner = inbiz.browser.designer;
  const field = useField<Field>();
  useEffect(() => {
    if (!isDesigner) {
      field.hidden = true;
    }
  }, []);

  return <Input {...others} style={{ textAlign: 'center' }} disabled={true} />;
};
EformHidden.displayName = 'EformHidden';
(EformHidden as typeof EformHidden & IDefaultValueFormat<typeof defaultProps>).defaultValueFormat =
  (props: any): string | void => {
    if (props.defaultValueType == 'custom' && props.customValue) {
      return props.customValue;
    } else if (props.defaultValueType == 'system' && props.systemDefaultValue) {
      const { userInfoByToken } = props.inbiz.appInfo;
      const data = {
        System_Var_LoginUserID: userInfoByToken.ID,
        System_Var_LoginUserIdentityId: userInfoByToken.IdentityId,
        System_Var_LoginUserName: userInfoByToken.Name,
        System_Var_LoginUserDeptID: userInfoByToken.MainDepartmentId,
        System_Var_LoginUserDeptName: userInfoByToken.MainDepartmentName,
        System_Var_LoginUserPostID: userInfoByToken.MainPositionId,
        System_Var_LoginUserPostName: userInfoByToken.MainPositionName,
        System_Var_LoginUserAccount: userInfoByToken.Account,
      };
      let result = '';
      for (const key in data) {
        if (key == props.systemDefaultValue) {
          result = data[key];
        }
      }
      return result;
    }
    if (!props.value) {
      return '';
    }
  };
export default connect(EformHidden);
